set
row/1*100000/
R  region/
$include ..\define/region17.set
WLD
/
SSP/SSP2,SSP3/
Srcp/RCP26,RCP85,nocc/
co2fert/co2,noco2,nocc/
returnpd/RP001,RP002,RP005,RP01,RP02,RP033,RP066,RP08,RP09,RP095,RP098,RP099/
SCENARIO /
*	SSP2_BaU_NoCC
	SSP2_BaU_ge2m_RCP26_noco2
	SSP2_BaU_ge2m_RCP85_noco2
	SSP2_BaU_hg2e_RCP26_noco2
	SSP2_BaU_hg2e_RCP85_noco2
	SSP2_BaU_ipsl_RCP26_noco2
	SSP2_BaU_ipsl_RCP85_noco2
	SSP2_BaU_mes__RCP26_noco2
	SSP2_BaU_mes__RCP85_noco2
	SSP2_BaU_ne1m_RCP26_noco2
	SSP2_BaU_ne1m_RCP85_noco2
*	SSP3_BaU_NoCC
	SSP3_BaU_ge2m_RCP26_noco2
	SSP3_BaU_ge2m_RCP85_noco2
	SSP3_BaU_hg2e_RCP26_noco2
	SSP3_BaU_hg2e_RCP85_noco2
	SSP3_BaU_ipsl_RCP26_noco2
	SSP3_BaU_ipsl_RCP85_noco2
	SSP3_BaU_mes__RCP26_noco2
	SSP3_BaU_mes__RCP85_noco2
	SSP3_BaU_ne1m_RCP26_noco2
	SSP3_BaU_ne1m_RCP85_noco2
/
Y	/2020,2030,2050/
A       /CALL/
Var/Proportion,Return/
Rreg(R)/
$include ..\define/region17.set
/
Sgcm/ge2m,hg2e,ipsl,mes_,ne1m/
MAP_SCEN(SCENARIO,SSP,Sgcm,Srcp,co2fert)/
*	SSP2_BaU_NoCC	.	SSP2	.	NoCC	.		.
	SSP2_BaU_ge2m_RCP26_noco2	.	SSP2	.	ge2m	.	RCP26	.	noco2
	SSP2_BaU_ge2m_RCP85_noco2	.	SSP2	.	ge2m	.	RCP85	.	noco2
	SSP2_BaU_hg2e_RCP26_noco2	.	SSP2	.	hg2e	.	RCP26	.	noco2
	SSP2_BaU_hg2e_RCP85_noco2	.	SSP2	.	hg2e	.	RCP85	.	noco2
	SSP2_BaU_ipsl_RCP26_noco2	.	SSP2	.	ipsl	.	RCP26	.	noco2
	SSP2_BaU_ipsl_RCP85_noco2	.	SSP2	.	ipsl	.	RCP85	.	noco2
	SSP2_BaU_mes__RCP26_noco2	.	SSP2	.	mes_	.	RCP26	.	noco2
	SSP2_BaU_mes__RCP85_noco2	.	SSP2	.	mes_	.	RCP85	.	noco2
	SSP2_BaU_ne1m_RCP26_noco2	.	SSP2	.	ne1m	.	RCP26	.	noco2
	SSP2_BaU_ne1m_RCP85_noco2	.	SSP2	.	ne1m	.	RCP85	.	noco2
*	SSP3_BaU_NoCC	.	SSP3	.	NoCC	.		.
	SSP3_BaU_ge2m_RCP26_noco2	.	SSP3	.	ge2m	.	RCP26	.	noco2
	SSP3_BaU_ge2m_RCP85_noco2	.	SSP3	.	ge2m	.	RCP85	.	noco2
	SSP3_BaU_hg2e_RCP26_noco2	.	SSP3	.	hg2e	.	RCP26	.	noco2
	SSP3_BaU_hg2e_RCP85_noco2	.	SSP3	.	hg2e	.	RCP85	.	noco2
	SSP3_BaU_ipsl_RCP26_noco2	.	SSP3	.	ipsl	.	RCP26	.	noco2
	SSP3_BaU_ipsl_RCP85_noco2	.	SSP3	.	ipsl	.	RCP85	.	noco2
	SSP3_BaU_mes__RCP26_noco2	.	SSP3	.	mes_	.	RCP26	.	noco2
	SSP3_BaU_mes__RCP85_noco2	.	SSP3	.	mes_	.	RCP85	.	noco2
	SSP3_BaU_ne1m_RCP26_noco2	.	SSP3	.	ne1m	.	RCP26	.	noco2
	SSP3_BaU_ne1m_RCP85_noco2	.	SSP3	.	ne1m	.	RCP85	.	noco2
/
Ragg/
US
LAM
EU
China
SAsia
SEAsia
Afr_ME
FSU
Other
World
/
Rhighincome(R)/
USA
XE25
XER
XOC
JPN
CAN
/
MAP_Ragg(R,Ragg)/
USA	.	US
XE25	.	EU
XER	.	EU
TUR	.	Other
XOC	.	Other
CHN	.	China
IND	.	SAsia
JPN	.	Other
XSE	.	SEAsia
XSA	.	SAsia
CAN	.	Other
BRA	.	LAM
XLM	.	LAM
CIS	.	FSU
XME	.	Afr_ME
XNF	.	Afr_ME
XAF	.	Afr_ME
USA	.	World
XE25	.	World
XER	.	World
TUR	.	World
XOC	.	World
CHN	.	World
IND	.	World
JPN	.	World
XSE	.	World
XSA	.	World
CAN	.	World
BRA	.	World
XLM	.	World
CIS	.	World
XME	.	World
XNF	.	World
XAF	.	World
/
;

parameter
	Foodshock(Y,R,Srcp,SSP,co2fert,returnpd,*)      tonne per year
	Foodstock(Y,R,Srcp,SSP,co2fert,returnpd)      tonne per year
	FoodstockRP(Y,R,Srcp,SSP,co2fert,returnpd)     return period
	FoodstockEJ(Y,R,Srcp,SSP,co2fert,returnpd)      EJ per year
	Foodstockkcal_POP(Y,R,Srcp,SSP,co2fert,returnpd)      EJ per person per year
	FoodstockRPagg(Y,Ragg,Srcp,SSP,co2fert,returnpd)     return period
	FoodstockEJagg(Y,Ragg,Srcp,SSP,co2fert,returnpd)      EJ per year
	Foodstockkcal_POPagg(Y,Ragg,Srcp,SSP,co2fert,returnpd)      EJ per person per year

	PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd)	kcal per day per capita
        PCAL_POP_ABS_CC0(SCENARIO,Y,R,A)    Per-capita calorie intake [kcal per person per day]
        PCAL_POP_ABS_CC(Srcp,SSP,co2fert,Y,R,A)    Per-capita calorie intake [kcal per person per day]
        PCAL_POP_ABS_CC_SSP2(SCENARIO,Y,R,A)    Per-capita calorie intake [kcal per person per day]
        PCAL_POP_ABS_CC_SSP3(SCENARIO,Y,R,A)    Per-capita calorie intake [kcal per person per day]
        POP(Srcp,SSP,co2fert,Y,R)               Population ratio of undernutrition [thausand]
        POP_SSP2(SCENARIO,Y,R)               Population ratio of undernutrition [thausand]
        POP_SSP3(SCENARIO,Y,R)               Population ratio of undernutrition [thausand]
	PRD_WHT(R)	wheat production in region R in 2005 [tonne per year]
	returnperiod(returnpd)	return period
	EJ_kcal
;
returnperiod("RP001")=100;
returnperiod("RP002")=50;
returnperiod("RP005")=20;
returnperiod("RP01")=10;
returnperiod("RP02")=5;
returnperiod("RP033")=3;

EJ_kcal = 4184/10**18;

PRD_WHT(R)=713182914;
$gdxin '../data/foodshockR.gdx'
$load foodshock

PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd)=foodshock(Y,R,Srcp,SSP,co2fert,returnpd,"return");

$gdxin '../data/final_results.gdx'
$load POP_SSP2=POP
$load PCAL_POP_ABS_CC_SSP2=PCAL_POP_ABS

$gdxin '../data/2050ssp3/final_results.gdx'
$load POP_SSP3=POP
$load PCAL_POP_ABS_CC_SSP3=PCAL_POP_ABS

POP(Srcp,SSP,co2fert,Y,R)$SUM(SCENARIO$MAP_SCEN(SCENARIO,SSP,"mes_",Srcp,co2fert),POP_SSP2(SCENARIO,Y,R))=SUM(SCENARIO$MAP_SCEN(SCENARIO,SSP,"mes_",Srcp,co2fert),POP_SSP2(SCENARIO,Y,R));
POP(Srcp,SSP,co2fert,Y,R)$SUM(SCENARIO$MAP_SCEN(SCENARIO,SSP,"mes_",Srcp,co2fert),POP_SSP3(SCENARIO,Y,R))=SUM(SCENARIO$MAP_SCEN(SCENARIO,SSP,"mes_",Srcp,co2fert),POP_SSP3(SCENARIO,Y,R));

PCAL_POP_ABS_CC0(SCENARIO,Y,R,A)$(PCAL_POP_ABS_CC_SSP2(SCENARIO,Y,R,A))=PCAL_POP_ABS_CC_SSP2(SCENARIO,Y,R,A);
PCAL_POP_ABS_CC0(SCENARIO,Y,R,A)$(PCAL_POP_ABS_CC_SSP3(SCENARIO,Y,R,A))=PCAL_POP_ABS_CC_SSP3(SCENARIO,Y,R,A);

PCAL_POP_ABS_CC(Srcp,SSP,co2fert,Y,R,A)=SUM(SCENARIO$MAP_SCEN(SCENARIO,SSP,"mes_",Srcp,co2fert),PCAL_POP_ABS_CC0(SCENARIO,Y,R,A));

Foodstockkcal_POP(Y,R,Srcp,SSP,co2fert,returnpd)$((not Rhighincome(R)) and PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd))=PCAL_POP_ABS_CC(Srcp,SSP,co2fert,Y,R,"CALL")-PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd);
FoodstockEJ(Y,R,Srcp,SSP,co2fert,returnpd)$(PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd) and (not Rhighincome(R))) = (PCAL_POP_ABS_CC(Srcp,SSP,co2fert,Y,R,"CALL")-PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd)) * POP(Srcp,SSP,co2fert,Y,R) * 10**3 * 365.2 * EJ_kcal;
FoodstockEJ(Y,"WLD",Srcp,SSP,co2fert,returnpd)=SUM(R$(Rreg(R) and (not Rhighincome(R))),FoodstockEJ(Y,R,Srcp,SSP,co2fert,returnpd));
FoodstockRP(Y,R,Srcp,SSP,co2fert,returnpd)$(FoodstockEJ(Y,R,Srcp,SSP,co2fert,returnpd)) = returnperiod(returnpd);


Foodstockkcal_POPagg(Y,Ragg,Srcp,SSP,co2fert,returnpd)$SUM(R$(PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd) and MAP_Ragg(R,Ragg) and (not Rhighincome(R)) and (PCAL_POP_ABS_CC(Srcp,SSP,co2fert,Y,R,"CALL")-PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd)) * POP(Srcp,SSP,co2fert,Y,R)),POP(Srcp,SSP,co2fert,Y,R))=
SUM(R$(PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd) and MAP_Ragg(R,Ragg) and (not Rhighincome(R))  and (PCAL_POP_ABS_CC(Srcp,SSP,co2fert,Y,R,"CALL")-PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd)) * POP(Srcp,SSP,co2fert,Y,R)),(PCAL_POP_ABS_CC(Srcp,SSP,co2fert,Y,R,"CALL")-PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd)) * POP(Srcp,SSP,co2fert,Y,R))
/SUM(R$(PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd) and MAP_Ragg(R,Ragg) and (not Rhighincome(R))  and (PCAL_POP_ABS_CC(Srcp,SSP,co2fert,Y,R,"CALL")-PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd)) * POP(Srcp,SSP,co2fert,Y,R)),POP(Srcp,SSP,co2fert,Y,R));

FoodstockEJagg(Y,Ragg,Srcp,SSP,co2fert,returnpd)= SUM(R$(PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd) and MAP_Ragg(R,Ragg) and (not Rhighincome(R))),(PCAL_POP_ABS_CC(Srcp,SSP,co2fert,Y,R,"CALL")-PCAL_POP_ABS_RP0(Y,R,Srcp,SSP,co2fert,returnpd)) * POP(Srcp,SSP,co2fert,Y,R) * 10**3 * 365.2 * EJ_kcal);
FoodstockRPagg(Y,Ragg,Srcp,SSP,co2fert,returnpd)$(FoodstockEJagg(Y,Ragg,Srcp,SSP,co2fert,returnpd)) = returnperiod(returnpd);

set
Rusda/
$include ..\define/region_usda.set
/
MAP_Rusda(Rusda,Ragg)/
$include ..\define/region_usda.map
/
Ausda/
"Wheat"
"Coarse graings"
"Corn"
"Rice"
/
;
parameter
kcal_g(Ausda)	conversion factor from gramme to kcal [kcal per gramme]
ActualStock0(Rusda,Ausda) [Mt]
ActualStock(Ragg)
;
kcal_g("Wheat")=535/183;
kcal_g("Coarse graings")=7/2.63;
kcal_g("Corn")=143/47.61;
kcal_g("Rice")=711/287.08;

$libinclude xlimport ActualStock0 ..\data\Actualstock_USDA.xlsx

ActualStock(Ragg)=SUM(Ausda,SUM(Rusda$MAP_Rusda(Rusda,Ragg),ActualStock0(Rusda,Ausda)*10**12*kcal_g(Ausda)*EJ_kcal));

Execute_unload '..\output\gdx\FoodStock.gdx'
PCAL_POP_ABS_RP0,
FoodstockEJ,Foodstockkcal_POP
FoodstockEJagg,Foodstockkcal_POPagg
FoodstockRP
FoodstockRPagg
ActualStock


;


;
